package com.tyae.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Html {

	HttpURLConnection httpConnection = null;

	protected String getConteudo(String url) throws UnsupportedEncodingException,IOException {

		try {

			StringBuffer dados = new StringBuffer();
			BufferedReader br = null;

			URL paginaURL = new URL(url);

			httpConnection = (HttpURLConnection) paginaURL.openConnection();
			httpConnection.setDoOutput(true);
			httpConnection.setDoInput(true);
			httpConnection.setUseCaches(false);
			httpConnection.setRequestProperty("Cookie", "");
			httpConnection.setRequestProperty("Request-Method", "GET");
			httpConnection.setRequestProperty("Accept-Charset",
					"ISO-8859-1,utf-8;q=0.7,*;q=0.7");

			// String charset = "ISO-8859-1";
			String charset = "UTF-8";

			httpConnection.setRequestProperty("Content-Encoding", charset);
			httpConnection.setRequestProperty("Content-type",
					"text/html; charset=" + charset);
			httpConnection.setRequestProperty("Content-Language", "pt-br");
			httpConnection.connect();

			dados.delete(0, dados.length());

			br = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), charset));
			// br = new BufferedReader(new
			// InputStreamReader(httpConnection.getInputStream()));

			String readLine;
			while (((readLine = br.readLine()) != null)) {
				dados.append(readLine + "\n");
			}

			return dados.toString();

		} finally {

			if (httpConnection != null) {
				httpConnection.disconnect();
			}

		}

	}

}
